package org.example.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import net.sf.ehcache.CacheManager;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.io.ResourceUtils;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.example.realm.MyRealmNew;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author 李云龙
 * @program: ShiroConfig
 * @description:
 * @date 2024-10-09 22:18:00
 */
@Configuration
public class ShiroConfig {
    @Autowired
    private MyRealmNew myRealmNew;

    /**
     * 创建并配置 DefaultWebSecurityManager 实例
     *
     * @return 配置好的 DefaultWebSecurityManager 实例
     */
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager() {
        // 创建 DefaultWebSecurityManager 实例
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();

        // 创建 HashedCredentialsMatcher 实例，用于密码匹配
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        // 设置哈希算法为 MD5
        matcher.setHashAlgorithmName("md5");
        // 设置哈希迭代次数为 3
        matcher.setHashIterations(3);

        // 设置 MyRealmNew 的 CredentialsMatcher
        myRealmNew.setCredentialsMatcher(matcher);

        // 将 MyRealmNew 设置到 DefaultWebSecurityManager 中
        manager.setRealm(myRealmNew);

        // 设置 RememberMeManager
        manager.setRememberMeManager(rememberMeManager());

        // 设置 CacheManager
        manager.setCacheManager(getEhCacheManager());

        // 返回配置好的 DefaultWebSecurityManager 实例
        return manager;
    }


    // 缓存管理器
    public EhCacheManager getEhCacheManager() {
        EhCacheManager ehCacheManager = new EhCacheManager();
        InputStream is = null;
        try {
            is = ResourceUtils.getInputStreamForPath("classpath:ehcache/ehcache.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        CacheManager cacheManager = new CacheManager(is);
        ehCacheManager.setCacheManager(cacheManager);
        return ehCacheManager;
    }

    // cookie属性设置
    public SimpleCookie rememberMeCookie() {
        SimpleCookie cookie = new SimpleCookie("rememberMe");
        // 设置跨域
        // cookie.setDomain(domain);
        cookie.setPath("/");
        cookie.setHttpOnly(true);
        cookie.setMaxAge(30 * 24 * 60 * 60);
        return cookie;
    }

    // 创建Shiro的cookie管理对象
    public CookieRememberMeManager rememberMeManager() {
        CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
        cookieRememberMeManager.setCookie(rememberMeCookie());
        cookieRememberMeManager.setCipherKey("1234567890987654".getBytes());
        return cookieRememberMeManager;
    }

    // config shiro 's built-in filter interception range
    @Bean
    public DefaultShiroFilterChainDefinition shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition();
        // set resources that can be accessed without authentication
//        definition.addPathDefinition("/swagger-ui.html", "anon");
//        definition.addPathDefinition("/swagger-resources/*", "anon");
//        definition.addPathDefinition("/v2/api-docs", "anon");
//        definition.addPathDefinition("/v2/webjars/**", "anon");
        definition.addPathDefinition("/myController/userLogin", "anon");
        definition.addPathDefinition("/myController/login", "anon");
        // set登出过滤器
        definition.addPathDefinition("/logout", "logout");
        // set resources that must be authenticated to access
        definition.addPathDefinition("/**", "authc");
        // 添加存在用户的过滤器（rememberMe）
        definition.addPathDefinition("/**", "user");
        return definition;
    }

    /**
     * 用于解析thymeleaf中的shiro相关属性
     * @return 返回一个ShiroDialect实例，用于在Thymeleaf模板中使用Shiro标签
     */
    @Bean
    public ShiroDialect shiroDialect() {
        // 创建并返回一个新的ShiroDialect实例
        return new ShiroDialect();
    }



}
